<template>
  <!--  <common-edit-dialog :title="dialogTitle" :visible.sync="dialogShow" width="800px" append-to-body>-->
  <common-edit-dialog
    :visible="dialogShow"
    :title="dialogTitle"
    :button-loading="loading"
    width="800px"
    @cancel="close('onlyClose')"
    @submit="submitForm"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      @submit.native.prevent
    >
      <el-row :gutter="18">
        <el-col :span="24" v-show="form.parentId != 0">
          <el-form-item label="上级组织" prop="parentId">
            <treeselect
              :options="deptselect"
              placeholder="请选择"
              :normalizer="normalizer"
              v-model="form.parentId"
              :disabled="true"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="组织名称" prop="deptName">
            <common-input
              v-model="form.deptName"
              :verify="false"
              :type="'text'"
              :maxlength="16"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <common-input-number
              :max-length="4"
              v-model="form.orderNum"
              controls-position="right"
            ></common-input-number>
          </el-form-item>
        </el-col>
        <template v-if="form.deptId">
          <el-col :span="12" v-show="false">
            <el-form-item label="负责人" prop="leader">
              <common-input v-model="form.leader" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="false">
            <el-form-item label="手机号" prop="phone">
              <common-input v-model="form.phone" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="false">
            <el-form-item label="邮箱" prop="email">
              <common-input v-model="form.email" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </template>
        <el-col :span="12" v-show="false">
          <el-form-item label="状态" prop="status">
            <el-radio v-model="form.status" label="0">正常</el-radio>
            <el-radio v-model="form.status" label="1">停用</el-radio>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <common-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8 }"
              v-model="form.remark"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </common-edit-dialog>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { dept_add, dept_edit } from "@/api/management/usercenter/user.js";
import { sortRule } from "@/utils/formRules";

export default {
  components: { Treeselect },
  props: {
    deptselect: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      dialogTitle: "新增", // 标题
      dialogShow: false, // 是否显示
      form: {
        orderNum: 1,
        status: "0",
      },
      rules: {
        parentId: [
          {
            required: true,
            message: "请选择上级组织",
            trigger: "blur",
          },
        ],
        deptName: [
          {
            required: true,
            message: "请输入组织名称",
            trigger: "blur",
          },
        ],
        orderNum: sortRule,

        // status: [
        //   {
        //     required: true,
        //     message: '请选择状态',
        //     trigger: 'blur'
        //   }
        // ]
      },
      loading: false,
    };
  },
  // watch: {
  //   dialogShow(val, oldVal) {
  //     if (!val) {
  //       this.$refs.form.resetFields();
  //       this.$emit("refresh");
  //     }
  //   },
  // },
  computed: {},
  methods: {
    normalizer(node) {
      // 去掉children=[]的children属性
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
      };
    },
    /**
     * 关闭
     */
    close(onlyClose) {
      this.$refs.form.resetFields();
      this.dialogShow = false;
      if (!onlyClose) {
        this.$emit("refresh");
      }
    },
    /**
     * 显示
     */
    show(data) {
      this.dialogShow = true;
      this.dialogTitle = "组织详情";
      this.dialogTitle = data.deptId ? "编辑组织" : "新增组织";

      // this.getDeptList();
      if (data.deptId) {
        data.orderNum = parseInt(data.orderNum);
        this.form = data;
      } else {
        this.form = this.$options.data().form;
        this.form.parentId = this.deptselect[0].deptId;
      }
    },
    /**
     * 提交
     */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (!!this.form.deptId) {
            dept_edit(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("编辑成功");
                  this.close();
                } else {
                  this.$GlobalErrorConfirm("编辑失败", res.msg).then();
                }
              })
              .finally(() => (this.loading = false));
          } else {
            dept_add(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("新增成功");
                  this.close();
                } else {
                  this.$GlobalErrorConfirm("新增失败", res.msg).then();
                }
              })
              .finally(() => (this.loading = false));
          }
        }
      });
    },
  },
  mounted() {},
};
</script>
<style lang="scss" scoped></style>
